package com.flycent.hqhs;

import net.tsz.afinal.FinalDb;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.app.Application;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TabHost;
import android.widget.Toast;

/**
 * @author fbl
 */
public class App extends Application{
	
	private static final String TAG = "com.flycent.hqhs";
	
	private static final boolean DBG = true;
	
	
	private static App mInstance = null;
	
	public static App getInstance(){
		if(mInstance == null ){
			mInstance = new App();
		}

		return mInstance;

	}
	
	//---------------------------------------------
	
	public FinalDb finalDb = null; 		// db
	private final int dbVersion = 1; 	// db版本

	private TabHost tabHost;
	
	public TabHost getTabHost() {
		return tabHost;
	}

	public void setTabHost(TabHost tabHost) {
		this.tabHost = tabHost;
	}	
	
	//---------------------------------------------

	@Override
	public void onCreate() {
		super.onCreate();

		/* 实例化全局异常捕获，并友好的形式表现出来，此处注销是为了调试*/
		//		CrashHandler.getInstance().init(getApplicationContext()); 
		mInstance = this;

		initImageLoader();
		finalDb = FinalDb.create(this, null, "Hqhs", false, dbVersion, null);
	}
	
	
	/* 在这里实例化图片缓存的ImageLoader的功能 */
	private void initImageLoader() {

		DisplayImageOptions defaultOptions = new DisplayImageOptions
				.Builder()
		.showImageForEmptyUri(R.drawable.default_img_0) 
		.showImageOnLoading(R.drawable.default_img_0)
		.showImageOnFail(R.drawable.empty_image) 
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration
				.Builder(getApplicationContext())
		.defaultDisplayImageOptions(defaultOptions)
		.discCacheSize(100 * 1024 * 1024)//
		.discCacheFileCount(150)//缓存一百张图片
		.writeDebugLogs()
		.build();
		ImageLoader.getInstance().init(config);

	}




	private static Handler mHandler = new Handler();
	private static Toast toast;
	public static void showShortToast(final CharSequence msg) {
		mHandler.post(new Runnable() {

			@Override
			public void run() {

				if (toast == null)
					toast = Toast.makeText(mInstance, msg, Toast.LENGTH_SHORT);
				else
					toast.setText(msg);

				toast.show();
			}
		});
	}
	public static void showShortToast(int resId) {
		showShortToast(mInstance.getString(resId));
	}

	/**
	 *打印Logs
	 * 
	 * @param msg
	 */
	public static void showLog(Object msg) {
		if (DBG)
			Log.i("hqhs", msg + "");
	}
	
	/**
	 * @param tag
	 * @param msg
	 */
	public static void debug(String tag, Object msg) {
		if (DBG)
			Log.d(TextUtils.isEmpty(tag) ? TAG : tag, String.valueOf(msg));
	}




}
